package cn.lcc.org.service.impl;



import cn.lcc.basic.service.impl.BaseServiceImpl;
import cn.lcc.basic.util.Md5Utils;
import cn.lcc.basic.util.StrUtils;
import cn.lcc.org.domain.Employee;
import cn.lcc.org.mapper.EmployeeMapper;
import cn.lcc.org.service.IEmployeeService;
import cn.lcc.user.domain.Logininfo;
import cn.lcc.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void save(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //【登录信息】创建Logininfo对象
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//0-管理员
        logininfo.setDisable(true);
        //将Logininfo对象添加到数据库，并生成自增id
        logininfoMapper.save(logininfo);//自增长id

        //【员工】将logininfo_id设置到employee对象中
        employee.setLogininfo_id(logininfo.getId());
        //添加到数据库
        super.save(employee);
        //employeeMapper.save(employee); //效果同上

        //【角色】为当前员工添加角色信息
        if(employee.getRole_id()!=null){
            employeeMapper.addRole(employee);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        /*
            外键：关联字段 + 外键约束
            如果只有关联字段，没有约束可以随便删
            但是如果有关联字段，也有外键约束 - 先删除关联信息，再删除当前表的数据
            开发中：先添加关联字段，不添加外键约束。开发完成之后将约束条件加进去。-方便开发时测试
         */
        //【角色】删除角色信息
        employeeMapper.delEmployeeRoleByEmployeeId(id);

        //查询employee对象
        Employee employee = employeeMapper.loadById(id);
        //【登录信息】删除logininfo信息
        logininfoMapper.del(employee.getLogininfo_id());

        //【员工】删除employee信息
        super.del(id);
    }

    @Override
    @Transactional
    public void update(Employee employee) {//修改的时候页面中不会传递logininfo_id和shop_id
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //【维护员工t_employee数据】查询到原来的employee对象
        Employee oldEmployee = employeeMapper.loadById(employee.getId());
        employee.setLogininfo_id(oldEmployee.getLogininfo_id());
        employee.setShop_id(oldEmployee.getShop_id());
        //更新数据t_employee
        super.update(employee);

        //【维护登录信息t_logininfo数据】更新数据t_logininfo
        Logininfo oldLogininfo = logininfoMapper.loadById(oldEmployee.getLogininfo_id());
        BeanUtils.copyProperties(employee,oldLogininfo);
        //属性同名原则会把员工的id拷贝给登录信息对象的id
        oldLogininfo.setId(oldEmployee.getLogininfo_id());
        logininfoMapper.update(oldLogininfo);

        //【维护角色t_employee_role数据】修改当前员工的角色
        if(employee.getRole_id()!=null){
            //删除角色信息
            employeeMapper.delEmployeeRoleByEmployeeId(employee.getId());
            //再添加新的角色信息
            employeeMapper.addRole(employee);
        }

    }
    @Override
    public void patchDel(Long[] ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public List<Employee> list(Long shopId) {
        return employeeMapper.loadByShopId(shopId);
    }
}